// https://blog.csdn.net/qq_34523482/article/details/120220333

import cache from '@/utils/cache.js'
import utils from '@/utils/utils.js'
import { setCount } from '@/api/api.js'

const returnFn = (fn) => {
	if (typeof fn === 'function') {
		return fn()
	}

	return fn
}

// 激励视频广告
let rewardedVideoAd = null
let rewarded = {
	load(callback = null) {
		console.log('rewardedVideoAd');
		if (!uni.createRewardedVideoAd) {
			console.log('rewardedVideoAd-not');
			return
		}

		let config = cache.get('ad_config')
		if (!config) {
			return returnFn(callback)
		}

		config = JSON.parse(config)
		if (!config.rewarded_id) {
			return returnFn(callback)
		}


		rewardedVideoAd = uni.createRewardedVideoAd({
			adUnitId: config.rewarded_id
		})
		// 激励视频广告加载
		rewardedVideoAd.onLoad(() => {})
		// 激励视频广告加载错误
		rewardedVideoAd.onError(() => {
			returnFn(callback)
		})
		// 激励视频广告关闭
		rewardedVideoAd.onClose((status) => {
			if (status && status.isEnded || status === undefined) {
				returnFn(callback)
			} else {
				return utils.Tips({
					title: '观看完广告才可以获取相应奖励哦'
				})
			}
		})
	},
	show(fn) {
		if (!rewardedVideoAd) {
			return fn()
		}

		uni.showModal({
			content: '观看完整视频广告获得奖励',
			success: ({
				confirm
			}) => {
				console.log(confirm);
				if (!confirm) {
					return
				}

				rewardedVideoAd.show()
					.catch(err => {
						rewardedVideoAd.load()
							.then(() => rewardedVideoAd.show())
							.catch(err => {
								console.log('rewardedVideoAd err', err)
								return fn()
							})
					})
			}
		})
	}
}

// 插屏广告
let interstitialAd = null
let interstitial = {
	load(callback = null) {
		if (!uni.createInterstitialAd) {
			return
		}

		let config = cache.get('ad_config')
		if (!config) {
			return returnFn(callback)
		}

		config = JSON.parse(config)
		if (!config.interstitial_id) {
			return returnFn(callback)
		}

		interstitialAd = uni.createInterstitialAd({
			adUnitId: config.interstitial_id
		})
		// 插屏广告加载
		interstitialAd.onLoad(() => {})
		// 插屏广告加载错误
		interstitialAd.onError(() => {
			return returnFn(callback)
		})
		// 插屏广告关闭
		interstitialAd.onClose(() => {
			return returnFn(callback)
		})
	},
	show(fn) {
		if (!interstitialAd) {
			return returnFn(fn)
		}

		interstitialAd.show().catch(err => {
			console.log('interstitialAd err', err);
			return returnFn(fn)
		})
	}
}

const call = (adType, fn) => {
	let config = cache.get('ad_config')
	console.log(adType)
	if (!config) {
		return returnFn(fn)
	}
	
	if(!config.interstitial_id && !config.rewarded_id){
		return returnFn(fn)
	}

	if(adType === 'download'){
		setCount(adType).then(({ count }) => {
			let userinfo = cache.get('userInfo')
			userinfo = JSON.parse(userinfo)
			
			if(userinfo.free_down_count && userinfo.free_down_count > count){
				return returnFn(fn)
			}
		});
	}

	config = JSON.parse(config)
	let type = `${adType}_ad`
	getAdLoad(parseInt(config[type]), fn)
}

const getAdLoad = (type, fn) => {
	try{
		if (type === 0) {
			if (Math.random() < 0.5) {
				interstitial.show(fn)
			} else {
				rewardedVideoAd.show(fn)
			}
		} else if (type === 1) {
			interstitial.show(fn)
		} else {
			rewardedVideoAd.show(fn)
		}
	}catch(e){
		console.log(e)
		return returnFn(fn)
	}
}

module.exports = {
	call,
	rewarded,
	interstitial
}